#include "my_list.h"
#include "my_hash_table.h"
#include "my_allocator.h"
#include "global_declarations.h"
#include <stdlib.h>


/**
 * creates and initializes a new hash table.
 */
HashTable* create_new_my_hash_table (){
	int i;
	HashTable* h = malloc(sizeof(HashTable));
	for (i = 0; i < 13; ++i) {
		h->buckets[i] = create_new_my_list();
	}
	return h;
}

/**
 * inserts a record to the hash table
 */
void insert_record_my_hash_table (HashTable* hash, unsigned int id, char* name) {
	int hashCode = hashFunction(id);
	MyList* bucket = hash->buckets[hashCode];
	insert_record_my_list(bucket, id, name);
}

/**
 * gets the record by the requested id.
 * returns 0 if not found.
 */
Record* retrieve_record_my_hash_table (HashTable* hash, unsigned int id) {
	int hashCode = hashFunction(id);
	MyList* bucket = hash->buckets[hashCode];
	return retrieve_record_my_list(bucket, id);
}

/**
 * resets the hash table
 */
void clear_all_my_hash_table (HashTable* hash) {
	int i;
	clear_my_allocator(get_allocator());
	for (i = 0; i < HASH_TABLE_SIZE; ++i) {
		clear_all_my_list(hash->buckets[i]);
	}
}

/**
 * the hash function
 */
int hashFunction(unsigned int id) {
	return id % 13;
}

